package com.gaohe.web.controller.dcp;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.gaohe.common.annotation.Log;
import com.gaohe.common.core.controller.BaseController;
import com.gaohe.common.core.domain.AjaxResult;
import com.gaohe.common.enums.BusinessType;
import com.gaohe.dcp.domain.DcpDoctor;
import com.gaohe.dcp.service.IDcpDoctorService;
import com.gaohe.common.utils.poi.ExcelUtil;
import com.gaohe.common.core.page.TableDataInfo;

import javax.xml.crypto.Data;

/**
 * 医生信息Controller
 *
 * @author OXXS
 * @date 2025-04-24
 */
@RestController
@RequestMapping("/dcp/doctor")
public class DcpDoctorController extends BaseController {
    @Autowired
    private IDcpDoctorService dcpDoctorService;

    //获取删除密码
    @PreAuthorize("@ss.hasPermi('dcp:patient:remove')")
    @PostMapping(value = "/pass")
    public AjaxResult getPassWord(@RequestBody String deletepass) {
        int status;
        try {
            // 使用 Jackson 将 JSON 字符串解析为 Map
            ObjectMapper objectMapper = new ObjectMapper();
            Map<String, String> requestBody = objectMapper.readValue(deletepass, Map.class);
            // 提取 password 的值
            String pass = requestBody.get("password");
            System.out.println(pass);
            String password = dcpDoctorService.selectPassWord();
            System.out.println(password);

            // 使用 BCrypt 校验明文密码和加密密码是否匹配
            BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
            if (passwordEncoder.matches(pass, password)) {
                System.out.println("密码匹配！");
                status = 1; // 成功状态码
            } else {
                System.out.println("密码不匹配！");
                status = 0;// 密码错误状态码
            }
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
//        HashMap<String, Integer> data = new HashMap<>();
//        data.put("status", status);
        return AjaxResult.success(status);
    }


    /**
     * 查询医生信息列表
     */
    @PreAuthorize("@ss.hasPermi('dcp:patient:list')")
    @GetMapping("/list")
    public TableDataInfo list(DcpDoctor dcpDoctor) {
        startPage();
        List<DcpDoctor> list = dcpDoctorService.selectDcpDoctorList(dcpDoctor);
//        System.out.println(list);
        return getDataTable(list);
    }

    /**
     * 导出医生信息列表
     */
    @PreAuthorize("@ss.hasPermi('dcp:doctor:export')")
    @Log(title = "医生信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DcpDoctor dcpDoctor) {
        List<DcpDoctor> list = dcpDoctorService.selectDcpDoctorList(dcpDoctor);
        ExcelUtil<DcpDoctor> util = new ExcelUtil<DcpDoctor>(DcpDoctor.class);
        util.exportExcel(response, list, "医生信息数据");
    }

    /**
     * 获取医生信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('dcp:doctor:query')")
    @GetMapping(value = "/{userId}")
    public AjaxResult getInfo(@PathVariable("userId") Long userId) {
        return success(dcpDoctorService.selectDcpDoctorByUserId(userId));
    }

    /**
     * 新增医生信息
     */
    @PreAuthorize("@ss.hasPermi('dcp:doctor:add')")
    @Log(title = "医生信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DcpDoctor dcpDoctor) {
        return toAjax(dcpDoctorService.insertDcpDoctor(dcpDoctor));
    }

    /**
     * 修改医生信息
     */
    @PreAuthorize("@ss.hasPermi('dcp:doctor:edit')")
    @Log(title = "医生信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DcpDoctor dcpDoctor) {
        return toAjax(dcpDoctorService.updateDcpDoctor(dcpDoctor));
    }

    /**
     * 删除医生信息
     */
    @PreAuthorize("@ss.hasPermi('dcp:doctor:remove')")
    @Log(title = "医生信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{userIds}")
    public AjaxResult remove(@PathVariable Long[] userIds) {
        return toAjax(dcpDoctorService.deleteDcpDoctorByUserIds(userIds));
    }
}
